/*
 * btnSearch.java
 *
 * Created on June 23, 2007, 4:00 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package latihanibatis.ui.event;

import com.ibatis.dao.client.DaoException;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JTextField;
import latihanibatis.dao.MahasiswaDao;
import latihanibatis.domain.Mahasiswa;
import latihanibatis.ui.MhsTabelModel;
import latihanibatis.ui.PanelStatus;

/**
 *
 * @author javamaniac
 */
public class BtnCari extends AbstractAction {
    
    private final javax.swing.JTextField txtAlamat;
    private final javax.swing.JTextField txtKeterangan;
    private final javax.swing.JTextField txtKota;
    private final javax.swing.JFormattedTextField txtNIM;
    private final javax.swing.JTextField txtNama;
    private final javax.swing.JLabel lblAlamat;
    private final javax.swing.JLabel lblKeterangan;
    private final javax.swing.JLabel lblKota;
    private final javax.swing.JLabel lblNIM;
    private final javax.swing.JLabel lblNama;
    private final JButton btnCek;
    private final JButton btnSearch;
    private final JButton btnSimpan;
    private final PanelStatus status;
    private final MhsTabelModel tableModel;
    private final MahasiswaDao mhsDao;
    
    /**
     * Creates a new instance of btnSearch
     */
    public BtnCari(JLabel lbNim, JFormattedTextField txt1, JLabel lblNm, JTextField txt2,
        JLabel lblKt, JTextField txt3, JLabel lblAlmt, JTextField txt4,
        JLabel lblKet, JTextField txt5, JButton btn, JButton btn1, JButton btn2,
        PanelStatus stat, MhsTabelModel tblMdl, MahasiswaDao dao) {
        
        super();
        this.lblNIM  = lbNim;
        this.txtNIM  = txt1;
        this.lblNama = lblNm;
        this.txtNama = txt2;
        this.lblKota = lblKt;
        this.txtKota = txt3;
        this.lblAlamat = lblAlmt;
        this.txtAlamat = txt4;
        this.lblKeterangan = lblKet;
        this.txtKeterangan = txt5;
        this.btnCek     = btn;
        this.btnSearch  = btn1;
        this.btnSimpan  = btn2;
        this.status     = stat;
        this.tableModel = tblMdl;
        this.mhsDao = dao;
    }
    
    public void actionPerformed(final ActionEvent evt) {
        tableModel.clearTableModel();
        Mahasiswa mhs = new Mahasiswa();
        Map<Object, Object> searchKeyword = new HashMap<Object, Object>();
        
        try {
            final int intNim = ((Number)txtNIM.getValue()).intValue();
            final String nama = txtNama.getText().toUpperCase();
            final String kota = txtKota.getText().toUpperCase();
            final String alamat = txtAlamat.getText().toUpperCase();
            final String keter  = txtKeterangan.getText().toUpperCase();
            
            searchKeyword.put("nim",  "%" + intNim + "%");
            searchKeyword.put("nama",  "%" + nama + "%");
            searchKeyword.put("kota",  "%" + kota + "%");
            searchKeyword.put("alamat",  "%" + alamat + "%");
            searchKeyword.put("keterangan",  "%" + keter + "%");
            
            final List searchResult = mhsDao.searchMhs(searchKeyword);
            int i = 0;
            for (i=0; i<searchResult.size(); i++) {
                mhs = (Mahasiswa) searchResult.get(i);
                
                tableModel.addMhs(mhs.getNim(), mhs.getNama(), mhs.getAlamat(),
                    mhs.getKota(), mhs.getKeterangan());
            }
            
            status.setStatus("Ditemukan " + i + " Data Mahasiswa !!!", "SUKSES");
            
            // Clearing all textField
            txtNIM.setValue(0);
            txtNama.setText("");
            txtKota.setText("");
            txtAlamat.setText("");
            txtKeterangan.setText("");
            
        } catch (DaoException excpt) {
            System.out.println(excpt.getLocalizedMessage());
            status.setStatus("Proses Pencarian Gagal !!!", "ERROR");
        }
    }
}
